package controllers.user;

import java.util.Collection;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import services.UserGroupService;
import services.UserService;
import controllers.AbstractController;
import domain.User;
import domain.UserGroup;
import forms.UserForm;

@Controller
@RequestMapping("/security/registration")
public class RegisterUserController extends AbstractController {
	// Services
	@Autowired
	private UserService userService;
	@Autowired
	private UserGroupService userGroupService;

	// Constructor
	public RegisterUserController() {
		super();
	}

	// Creation
	@RequestMapping(value = "/user", method = RequestMethod.GET)
	public ModelAndView registration() {
		ModelAndView result;
		UserForm userForm;
		Boolean isAdministrator;
		Collection<UserGroup> userGroups;

		userGroups = userGroupService.findAll();
		isAdministrator = false;
		userForm = new UserForm();

		result = new ModelAndView("security/registration/user");
		result.addObject("userForm", userForm);
		result.addObject("isAdministrator", isAdministrator);
		result.addObject("userGroups", userGroups);

		return result;
	}

	// Edition
	@RequestMapping(value = "/user", method = RequestMethod.POST, params = "save")
	public ModelAndView save(@Valid UserForm userForm, BindingResult binding) {
		ModelAndView result;
		User user;

		Boolean isAdministrator;
		Collection<UserGroup> userGroups;

		if (binding.hasErrors()) {
			result = createEditModelAndView(userForm);
		} else {
			try {
				if (!userForm.getAcceptConditions()) {
					result = createEditModelAndView(userForm,
							"security.commit.noAcceptConditions");
				} else if (!userForm.getPassword().equals(
						userForm.getRepeatPassword())) {
					result = createEditModelAndView(userForm,
							"security.commit.passwordDifferents");
				} else {

					user = userService.reconstruct(userForm);

					userService.save(user);
					result = new ModelAndView("redirect:../../");
				}
			} catch (Throwable oops) {

				result = createEditModelAndView(userForm, "user.commit.error");
			}
		}
		isAdministrator = false;
		userGroups = userGroupService.findAll();
		result.addObject("userGroups", userGroups);
		result.addObject("isAdministrator", isAdministrator);
		return result;
	}

	// Ancillary methods
	protected ModelAndView createEditModelAndView(UserForm userForm) {
		ModelAndView result;

		result = createEditModelAndView(userForm, null);

		return result;
	}

	protected ModelAndView createEditModelAndView(UserForm userForm,
			String message) {
		ModelAndView result;

		result = new ModelAndView("security/registration/user");

		result.addObject("userForm", userForm);
		result.addObject("message", message);

		return result;
	}
}
